//
//  baseViewController.swift
//  ShangHaiWeiBo
//
//  Created by 蔡杰 on 16/2/23.
//  Copyright © 2016年 蔡杰. All rights reserved.
//

import UIKit

import HMRefresh
class baseViewController: UIViewController {

    //懒加载一个tableview
    var tableView : UITableView?
    //懒加载一个读条
    var refreshControl : HMRefreshControl?
   
//    lazy var isView = false
    //四个控制的访客视图各自独立，互补干扰！
    var visitor : VisitorViewController?
    
    override func loadView() {
       
        UserAccount.sharedUserAccount.isLogin ? setTableView() : setVisitor()
        

    }
    //加载数据，在父类中设置方法，在子类中实现
        func loadData(){
    
        
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        if UserAccount.sharedUserAccount.isLogin {
        
            loadData()
        } else {
        
            
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginSuccessed", name: CZWeiBoLoginSuccessedNotification, object: nil)
        }

        // Do any additional setup after loading the view.
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @objc private func loginSuccessed() {
        
        view = nil
        
        // 注销通知
        NSNotificationCenter.defaultCenter().removeObserver(self, name: CZWeiBoLoginSuccessedNotification, object: nil)
    }


    
}
//MARK: 设置界面
extension baseViewController :VisitorViewDelegate{

    private func setVisitor(){
    
        visitor = VisitorViewController()

        visitor?.delegate = self
        view = visitor
//         设置状态栏按钮 - 如果换行参数，一定保证一个参数一行
        navigationItem.leftBarButtonItem = UIBarButtonItem(
            title: "注册",
            style: .Plain,
            target: self,
            action: "visitorViewDidRegister")
        navigationItem.rightBarButtonItem = UIBarButtonItem(
            title: "登录",
            style: .Plain,
            target: self,
            action: "visitorViewDidLogin")
        
    }

    private func setTableView(){
     //把所以继承这个类的View设置为Tableview
        tableView = UITableView(frame: CGRectZero, style: .Plain)
        view = tableView
        
        tableView?.delegate = self
        tableView?.dataSource = self

       
    }
    func setWard(){
    
        guard let tableView = tableView else {

            return
        }
        
        if refreshControl != nil {
        
            return
        }
        
        refreshControl = HMRefreshControl()
        tableView.addSubview(refreshControl!)
        
        refreshControl?.pulldownView = CZPulldownRefreshView.refeshView()
        refreshControl?.normalString = "下拉起飞"
        refreshControl?.pullingString = "放开起飞"
        refreshControl?.refreshingString = "正在起飞"
        refreshControl?.lastRefreshString = "末次 "
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
        
    }
    
}
//MARK:实现Visitor的代理方法
extension baseViewController {

    func visitorViewDidRegister() {
        print("注册")
    }
    func visitorViewDidLogin() {
        print("登陆")
        
        let oauthViewVc:OAuthViewController = OAuthViewController()
        
        let nav = UINavigationController(rootViewController: oauthViewVc)
        presentViewController(nav, animated: true, completion: nil)
    }

}
// MARK:设置数据源和代理
extension baseViewController: UITableViewDataSource ,UITableViewDelegate{
    //数据源方法
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 0
      
    }
    
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
      return UITableViewCell()
    }
    
    }